#include <stdio.h>
#include <string.h>
#include "esp_partition.h"
#include "esp_log.h"
#include "esp_check.h"

#define PARTITION_TYPE_01   0x40
#define PARTITION_TYPE_02   0x41
#define USER_01_SUBTYPE_01  0x01
#define USER_02_SUBTYPE_01  0x01

static const char* TAG = "partition_test";
esp_partition_t* partition_ptr = NULL;

void app_main(void)
{
    partition_ptr = esp_partition_find_first(PARTITION_TYPE_01, USER_01_SUBTYPE_01, NULL);
    if(partition_ptr == NULL){
        ESP_LOGE(TAG, "Find Partition Error\n");
        return;
    }
    char buffer[64];
    memset(buffer, 0, strlen(buffer));

    ESP_ERROR_CHECK(esp_partition_erase_range(partition_ptr, 0, 0x1000));
    ESP_ERROR_CHECK(esp_partition_write(partition_ptr, 0, "Hello world", strlen("Hello world")));
    ESP_ERROR_CHECK(esp_partition_read(partition_ptr, 0, buffer, strlen("Hello world")));
    ESP_LOGE(TAG, "Data read from user_1_partition is %s\n", buffer);

    partition_ptr = esp_partition_find_first(PARTITION_TYPE_02, USER_02_SUBTYPE_01, NULL);
    if(partition_ptr == NULL){
        ESP_LOGI(TAG, "Find Partition Error\n");
        return;
    }
    
    memset(buffer, 0, strlen(buffer));
    ESP_ERROR_CHECK(esp_partition_erase_range(partition_ptr, 0, 0x1000));
    ESP_ERROR_CHECK(esp_partition_write(partition_ptr, 0, "Hello world", strlen("Hello world")));
    ESP_ERROR_CHECK(esp_partition_read(partition_ptr, 0, buffer, strlen("Hello world")));
    ESP_LOGI(TAG, "Data read from user_1_partition is %s\n", buffer);

    return;
}
